
libname hcup2009 '';
libname seddwork '';

data states;
input state $2. fips 2. hospid 1.;
cards;
AZ040
HI151
IA190
KY211 
NJ340
NY360
UT490
WI550
run;


**Import list of chronic illness diagnosis codes;
**Downloaded from https://www.hcup-us.ahrq.gov/toolssoftware/chronic/chronic.jsp on 8/6/2014;

PROC IMPORT OUT= WORK.CCI 
            DATAFILE= "cci2012.csv" 
            DBMS=CSV REPLACE;
     GETNAMES=YES;
     DATAROW=2; 
RUN;

data cci;
set cci;
DX1=ICD_9_CM_CODE;
run;

proc sort data=cci;
by dx1;
quit;

**Import cost to charge ratio file;
**Downloaded from https://www.hcup-us.ahrq.gov/db/state/costtocharge.jsp on 8/6/2014;

PROC IMPORT OUT= WORK.CostChargeRatio 
            DATAFILE= "cc2009SID.csv" 
            DBMS=CSV REPLACE;
     GETNAMES=YES;
     DATAROW=2; 
RUN;

proc sort data=costchargeratio;
by hospid;
run;

data costchargeratio (drop=hospid);
set costchargeratio;
hospid2=hospid*1;
run;

data costchargeratio (drop=hospid2);
set costchargeratio;
hospid=hospid2;
run;

%macro sedd2009;

data _null_;
set states;
if _n_=&i then do;
call symput('state', state);
call symput('fips', fips);
call symput('hospid', hospid);
end;


%put &state;
%put &fips;

data seddwork.&state._sedd_2009_core;
set hcup2009.&state._sedd_2009_core;
FIPS=&fips;
run;

%if &hospid=0 %then %do;

proc sort data=seddwork.&state._sedd_2009_core;
by dshospid;
quit;

data seddwork.&state._sedd_2009_core;
merge seddwork.&state._sedd_2009_core seddwork.&state._sedd_2009_ahal;
by dshospid;
run;

%end;

%if &i=1 %then %do;

data seddwork.sedd2009;
set seddwork.&state._sedd_2009_core;
run;

%end;

%else %do;

data seddwork.sedd2009;
set seddwork.sedd2009 seddwork.&state._sedd2009_core;
run;

%end;

x rm -f seddwork.&state._sedd_2009_core;

%end;

data seddwork.sedd2009;
set seddwork.sedd2009;
SEX=FEMALE+1;
count=1;
bdate=trim(left(bmonth))||trim(left(byear));

*Define payers;

if pay1 LT 3 then public=1;
else public=0;
if pay1=3 then private=1;
else private=0;
if pay1=4 then self=1;
else self=0;
if pay1=5 then freecare=1;
else freecare=0;

*Define Dx categories;


ThreeDigDx2=substr(trim(left(dx1)),1,1);

if ThreeDigDx2="E" then ecode=1;
else ecode=0;
if ThreeDigDx2="V" then ThreeDigDx=substr(DX2,1,3)*1;

else ThreeDigDx=substr(DX1,1,3)*1;
if ThreeDigDx LE 679 and ThreeDigDx GE 630 then preg=1;
else preg=0;

if threeDigDx LE 319 and ThreeDigDx GE 290 then mental=1;
else mental=0;
if threeDigDX GE 800 and threeDigDX LT 830 then fracture=1;
else fracture=0;
if threeDigDX GE 800 and threeDigDX LT 960 then injury=1;
else injury=0;
if threeDigDX=. then fracture=.;
if threeDigDX=. then mental=.;
if threeDigDX=. then injury=.;


if ThreeDigDx=540 then Appendicitis=1;
else Appendicitis=0;

if threedigdx GT 0 and threedigdx LE 139 then infect=1;
else infect=0;
if threedigdx GT 139 and threedigdx LE 239 then neoplasms=1;
else neoplasms=0;
if threedigdx GT 239 and threedigdx LE 279 then endometa=1;
else endometa=0;
if threedigdx GT 279 and threedigdx LE 289 then blood=1;
else blood=0;
if threedigdx GT 289 and threedigdx LE 319 then mentaldisorders=1;
else mentaldisorders=0;
if threedigdx GT 319 and threedigdx LE 359 then nervoussys=1;
else nervoussys=0;
if threedigdx GT 359 and threedigdx LE 389 then sense=1;
else sense=0;
if threedigdx GT 389 and threedigdx LE 459 then circulatory=1;
else circulatory=0;
if threedigdx GT 459 and threedigdx LE 519 then respiratory=1;
else respiratory=0;
if threedigdx GT 519 and threedigdx LE 579 then digestive=1;
else digestive=0;
if threedigdx GT 579 and threedigdx LE 629 then genitourinary=1;
else genitourinary=0;
if threedigdx GT 679 and threedigdx LE 709 then skin=1;
else skin=0;
if threedigdx GT 709 and threedigdx LE 739 then muscle=1;
else muscle=0;
if threedigdx GT 739 and threedigdx LE 759 then congenital=1;
else congenital=0;
if threedigdx GT 759 and threedigdx LE 779 then perinatal=1;
else perinatal=0;
if threedigdx GT 779 and threedigdx LE 799 then illdefined=1;
else illdefined=0;
if threedigdx GT 799 and threedigdx LE 999 then injpois=1;
else injpois=0;
if preg=1 then delete;
run;

proc sort data=seddwork.sedd2009;
by DX1;
run;

data seddwork.sedd2009;
merge seddwork.sedd2009 cci;
by DX1;
if count=. then delete;
Chronic=CATEGORY_DESCRIPTION*1;
run;

proc sort data=seddwork.sedd2009;
by hospid;
run;

data seddwork.sedd2009;
merge seddwork.sedd2009 costchargeratio;
by hospid;
if count=. then delete;
cost=TOTCHG*APICC;
run;

proc print data=seddwork.sedd2009 (Obs=50);
quit;

proc means data=seddwork.sedd2009 N NMISS MIN MAX MEAN SUM;
quit;

proc sort data=seddwork.sedd2009;
by fips;
quit;

proc means data=seddwork.sedd2009 N NMISS MIN MAX MEAN SUM;
by fips;
quit;

%include 'addupsedd_restat.sas';

%mend sedd2009;
%sedd2009
